package com.niit.dao.impl;

import com.niit.dao.INewsDao;
import com.niit.domain.Carousel;
import com.niit.domain.News;
import com.niit.domain.Product;
import com.niit.utils.DBHelper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class NewsDaoImpl implements INewsDao {
    @Override
    public List<News> queryNews(Integer pageSize) {
        List<News> list=new ArrayList<>();
        //编写sql语句
        String sql="select * from sh_news where type = 0 order by pubdate desc limit ?";
        //将数据存放到结果集
        ResultSet rs= DBHelper.executeQuery(sql, pageSize);
        while (true){
            try {
                if (!rs.next()) break;
            } catch (SQLException e) {
                e.printStackTrace();
            }
            News news = null;
            try {
                news = new News(rs.getInt(1),rs.getString(2), rs.getInt(3),rs.getDate(4),rs.getInt(5));
            } catch (SQLException e) {
                e.printStackTrace();
            }
            list.add(news);
        }
        return list;
    }

    @Override
    public List<News> queryNews2(Integer pageSize) {
        List<News> list=new ArrayList<>();
        //编写sql语句
        String sql="select * from sh_news where type = 1 order by click_num desc limit ?";
//        System.out.println(sql);
        //将数据存放到结果集
        ResultSet rs= DBHelper.executeQuery(sql,pageSize);
        while (true){
            try {
                if (!rs.next()) break;
            } catch (SQLException e) {
                e.printStackTrace();
            }
            News news = null;
            try {
                news = new News(rs.getInt(1),rs.getString(2), rs.getInt(3),rs.getDate(4),rs.getInt(5));
            } catch (SQLException e) {
                e.printStackTrace();
            }
            list.add(news);
        }
        return list;
    }

    @Override
    public List<News> queryNews3(Integer pageSize) {
        List<News> list=new ArrayList<>();
        String sql="select * from sh_news where type=0 order by click_num desc limit ?";
        ResultSet rs=DBHelper.executeQuery(sql,pageSize);
        News news=null;
        while(true){
            try {
                if (!rs.next()) break;
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                news=new News(rs.getInt(1),rs.getString(2), rs.getInt(3),rs.getDate(4),rs.getInt(5));
            } catch (SQLException e) {
                e.printStackTrace();
            }
            list.add(news);
        }
        return list;
    }

    @Override
    public List<News> queryAllNews(Integer currentPage, Integer pageSize) {
        List<News> list=new ArrayList<>();
        String sql="select * from sh_news order by pubdate desc limit ?,?";
        ResultSet rs=DBHelper.executeQuery(sql,currentPage,pageSize);
        News news=null;
        while(true){
            try {
                if (!rs.next()) break;
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                news=new News(rs.getInt(1),rs.getString(2), rs.getInt(3),rs.getDate(4),rs.getInt(5));
            } catch (SQLException e) {
                e.printStackTrace();
            }
            list.add(news);
        }
        return list;
    }

    @Override
    public int queryNewsTotal() {
        String sql="select count(*) from sh_news";
        ResultSet rs = DBHelper.executeQuery(sql);
        int i=0;
        try {
            if(rs.next()){

                i = rs.getInt(1);

            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return i;
    }

    @Override
    public News queryNewsByID(Integer id) {
        News news = null;
        String sql = "select * from sh_news where id = ?";
        ResultSet rs = DBHelper.executeQuery(sql, id);
        try {
            if(rs.next()){
               news = new News(rs.getInt(1),rs.getString(2),rs.getInt(3),rs.getDate(4),rs.getInt(5));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return news;
    }

    @Override
    public int NewsCountadd(Integer id) {
        String sql = "update sh_news set click_num = click_num+1 where id = ?";
        int i = DBHelper.executeUpdate(sql,id);
        return i;
    }
}
